' Draw 10 Red Balloons each with a GREEN string ' The balloons hover above the brown earth. SCREEN 12 ' start up graphics ' LET BALLOON = 1 ' start with balloon number 1 DO WHILE BALLOON <= 10 ' end with balloon number 10 LET X = BALLOON * 64 - 32 ' calculate an X for this balloon ' COLOR 4 ' set the pen color to red CIRCLE (X, 100), 25 ' draw a balloon ' COLOR 2 ' set the pen color to green LINE (X, 125) - (X,225) ' draw its string LET BALLOON = BALLOON + 1 ' go on to next balloon LOOP ' COLOR 6 ' set the pen color to brown LINE (0, 240)-(639,240) ' draw a line across the screen END
You could have drawn the line first, before all the balloons.
This would be fine.
It would be a mistake to put it inside the loop (between the DO
and the LOOP
)
since this would draw it again and again, needlessly.
You may think this program is getting to be a bit overblown. But let's make one more addition, then we'll be done. It would be nice to put a little action into the picture. In a later chapter we will do much more of this, but right now it is easy to add one statement that makes the program more interesting.
The SLEEP
statement tells the computer to do nothing for a few
seconds:
SLEEP numberOfSeconds
For example, look at the following program:
' Sleep for a while after each arithmetic statement ' LET VALUE = 45 ' put 45 in VALUE SLEEP 2 ' do nothing for two seconds LET EXTRA = 25 ' put 25 in EXTRA SLEEP 10 ' do nothing for 10 seconds LET SUM = VALUE + EXTRA ' add VALUE plus EXTRA and put the result in SUM SLEEP 5 ' do nothing for 5 seconds PRINT "That was Hard! The sum is:", SUM ' write out the result END
The example program is silly; there is no good reason for using the
SLEEP
statements in it
(except maybe to make it look like the computer
took lots of time to solve this hard problem!)
An interesting place for the SLEEP
statement is in the balloon program.
If the computer did nothing for one second after drawing each balloon and
string, you would see the picture "unfold" as it was being created.
Here is the program so far:
' Draw 10 Red Balloons each with a GREEN string ' The balloons hover above the brown earth. SCREEN 12 ' start up graphics ' LET BALLOON = 1 ' start with balloon number 1 DO WHILE BALLOON <= 10 ' end with balloon number 10 LET X = BALLOON * 64 - 32 ' calculate an X for this balloon ' COLOR 4 ' set the pen color to red CIRCLE (X, 100), 25 ' draw a balloon ' COLOR 2 ' set the pen color to green LINE (X, 125) - (X,225) ' draw its string LET BALLOON = BALLOON + 1 ' go on to next balloon LOOP ' COLOR 6 ' set the pen color to brown LINE (0, 240)-(639,240) ' draw a line across the screen END
Modify the program by adding one SLEEP
statement so
that the program does nothing for one second after drawing
each balloon-and-string.
Consider carefully where the statement should go.